#include <string.h>
#include <iostream>
using std ::cout;
using std ::endl;
class Computer
{
public:
    static Computer *getInstance()
    {
        if (_pInstance == nullptr)
            _pInstance = new Computer();
        return _pInstance;
    }
    static void destroy()
    {
        if (_pInstance)
        {
            delete _pInstance;
            _pInstance = nullptr;
        }
        cout << "heap delete" << endl;
    }
    void init(const char *brand, double price)
    {
        if (_brand)
        {
            delete[] _brand;
            _brand = nullptr;
        }
        _brand = new char[strlen(brand) + 1]();
        strcpy(_brand, brand);
        _price = price;
    }
    void print()
    {
        cout << _brand << endl;
        cout << _price << endl;
    }

private:
    // 构造函数
    Computer() {};
    Computer(const char *brand, double price)
        : _brand(new char[strlen(brand) + 1]()), _price(price)
    {
        strcpy(_brand, brand);
    }
    // 析构函数
    ~Computer()
    {
        if (_brand)
        {
            delete _brand;
            _brand = nullptr;
        }
        cout << "~Computer" << endl;
    }
    Computer(const Computer &rhs) = delete;
    Computer &operator=(const Computer &rhs) = delete;
    char *_brand;
    double _price;
    static Computer *_pInstance;
};
Computer *Computer ::_pInstance = nullptr;

int main()
{
    Computer ::getInstance()->init("bob", 2222);
    Computer ::getInstance()->print();
    Computer ::getInstance()->init("tom", 6666);
    Computer ::getInstance()->print();
    Computer ::destroy();
    return 0;
}